//
//  Test002Controller.m
//  testnav
//
//  Created by IOS3 on 2018/11/6.
//  Copyright © 2018 IOS3. All rights reserved.
//

#import "Test002Controller.h"
#import "Test003Controller.h"


@interface Test002Controller ()
@property (nonatomic, strong) UIImage *backgroundImage;
@property (nonatomic, strong) UIImageView *navImageView;
@property (nonatomic, strong) UIView *rightBarItem;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, assign) CGFloat navAlpha;
@end

#define TOP_HEIGHT 88
#define SCREEN_WIDTH UIScreen.mainScreen.bounds.size.width
#define SCREEN_HEIGHT UIScreen.mainScreen.bounds.size.height
@implementation Test002Controller

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"test";
    self.navAlpha = 0.f;
    [self.tableVeiw registerClass:[UITableViewCell class] forCellReuseIdentifier:@"TTTTtestControllerCellID"];
    [self setupHaderView];
    [self setNavItems];
    
}


- (void)setNavItems {
    
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, 100, 40);
    view.backgroundColor = UIColor.clearColor;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.rightBarItem = view;
    
    
    UIButton *btn = [[UIButton alloc] init];
    [btn setTitle:@"标题" forState:UIControlStateNormal];
    [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 100, 40);
    [view addSubview:btn];
    self.rightButton = btn;
    
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnClick {
    [self.navigationController pushViewController:Test003Controller.new animated:YES];
}

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}



- (void)setupHaderView {
    
    self.tableVeiw.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.tableVeiw.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -0, SCREEN_WIDTH, 300)];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.backgroundColor = UIColor.blueColor;
    imageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);
    [view addSubview:imageView];
    self.tableVeiw.tableHeaderView = view;
    
    
//    UIColor *color = [UIColor colorWithWhite:1.0 alpha:1.f];
//    self.backgroundImage = [self imageWithColor:UIColor.whiteColor size:self.navigationController.navigationBar.bounds.size];
    
    
//    [self.navigationController.navigationBar setBackgroundImage:self.backgroundImage forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:UIImage.new];
    
    self.navImageView = self.navigationController.navigationBar.subviews.firstObject;
    
    
    
    
    
    
    
    
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navImageView.alpha = self.navAlpha;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (offsetY > TOP_HEIGHT) {
        offsetY = TOP_HEIGHT;
        self.rightBarItem.backgroundColor = UIColor.redColor;
        [self.rightButton setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    }
    else {
        self.rightBarItem.backgroundColor = UIColor.clearColor;
        [self.rightButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    }
    CGFloat a = offsetY / (TOP_HEIGHT + 1);
    self.navImageView.alpha = a;
    NSLog(@">>>> %f", a);
    
    self.navAlpha = a;
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TTTTtestControllerCellID" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"section: %ld - row: %ld", indexPath.section, indexPath.row];
    cell.detailTextLabel.text = @"tesashfauljh";
    return cell;
}

@end
